package common.codeGen;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class GenCreateDto {
	@Autowired
	private GenService genService;
	
	public String create(GenBean genBean){
		StringBuffer sb = new StringBuffer();
		try {
			List<GenColumn> tableNameList = genService.getTableColumnName(genBean.getTableName());
			if(tableNameList==null||tableNameList.size()<=0){
				throw new IllegalArgumentException();
			}
			
			sb.append("package "+genBean.getPackagePath()+".dto;").append("\n");
			sb.append("\n");
			
			sb.append("public class "+genBean.getTableName()+"{\n");
			sb.append(createPro(tableNameList));
			sb.append(createProMethod(tableNameList));
			sb.append("}\n");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sb.toString();
	}
	
	public String createPro(List<GenColumn> cols){
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<cols.size();i++){
			GenColumn col = cols.get(i);
			sb.append("private String ").append(col.getColumn_name()).append(";\n");
		}
		return sb.toString();
	}
	
//	public void setId(String id) {
//		this.id = id;
//	}
	public String createProMethod(List<GenColumn> cols){
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<cols.size();i++){
			String colName = cols.get(i).getColumn_name();
			String _colName = colName.substring(0,1).toUpperCase()+colName.substring(1);
			sb.append("public String get"+_colName+"() {\n");
			sb.append("return "+colName+";\n");
			sb.append("}\n");
			
			sb.append("public void set"+_colName+"(String "+colName+") {\n");
			sb.append("this."+colName+" = "+colName+";\n");
			sb.append("}\n");
		}
		return sb.toString();
	}
}
